$(function () {
    layui.use(['table', 'util'], function () {
        var table = layui.table, util = layui.util;
        table.render({
            elem: '#admin'
            , url: '../../news/getAllNews',
            method: 'POST'
            , even: true
            , cols: [[
                {field: 'id', title: 'ID', width: 80}
                , {field: 'title', title: '资讯标题'}
                , {field: 'content', title: '资讯内容'}
                // ,{field:'gallery',  title: '图册'}
                , {
                    field: 'addtime', title: '增加时间', templet: function (d) {
                        return util.toDateString(d.addtime, "yyyy-MM-dd HH:mm:ss");
                    }
                }
                , {toolbar: '#barDemo', title: '操作'}
            ]]
            , page: false,
        });
        table.on('tool(event)', function (obj) {
            if (obj.event === 'del') {
                layer.confirm('确定删除该资讯吗,删除后不可撤回', function (index) {
                    var data = obj.data;
                    $.ajax({
                        type: "POST",
                        url: "../../news/deleteNews",
                        data: {
                            "news_id": data.id
                        },
                        dataType: "json",
                        success: function (data) {
                            if (data.code == 0) {
                                obj.del();
                                layer.close(index);
                            }
                            layer.msg(data.msg);
                        }
                    });
                });
            } else if (obj.event == 'detail') {
                localStorage.setItem("news", JSON.stringify(obj.data));
                layer.open({
                    type: 2,
                    title: '资讯详情',
                    content: 'newsDetail.html',
                    maxmin: false,
                    area: ['800px', '600px'],
                    end: function() {
                    }
                });
            } else {
                localStorage.setItem("news", JSON.stringify(obj.data));
                layer.open({
                    type: 2,
                    title: '编辑资讯',
                    content: 'newsEdit.html',
                    maxmin: false,
                    area: ['800px', '600px'],
                    end: function() {
                    }
                });
            }
        })
    });
})